﻿Namespace Security
    Friend Class Handler
        Public Shared Sub SetupSystemGroups()
            Dim clsSystemGroupAuthenticatedUsers As Entities.SecurityGroup
            Using t As New Data.TransactionScope
                clsSystemGroupAuthenticatedUsers = GetByTypeAndSubType(Entities.SecurityGroupType.System, Entities.SecurityGroupSubType.AuthenticatedUsers)
                If clsSystemGroupAuthenticatedUsers Is Nothing Then
                    clsSystemGroupAuthenticatedUsers = New Entities.SecurityGroup
                    clsSystemGroupAuthenticatedUsers.Name = "Authenticated Users"
                    clsSystemGroupAuthenticatedUsers.Description = "All buddies"
                    clsSystemGroupAuthenticatedUsers.Type = Entities.SecurityGroupType.System
                    clsSystemGroupAuthenticatedUsers.SubType = Entities.SecurityGroupSubType.AuthenticatedUsers
                    DBController.Save(clsSystemGroupAuthenticatedUsers, t)

                    Dim aBuds As Generic.List(Of Entities.Buddy) = Buddies.Handler.GetAll
                    For Each clsBud As Entities.Buddy In aBuds
                        DBController.AddGroupMember(clsSystemGroupAuthenticatedUsers.Id, clsBud.Id, t)
                    Next
                End If
                t.Complete()
            End Using
        End Sub

        Public Shared Sub Save(ByVal group As Entities.SecurityGroup, ByVal transactionScope As Data.TransactionScope)
            DBController.Save(group, transactionScope)
            transactionScope.RegisterOnCompleteCallback(AddressOf OnUpdated_TransactionCallback, "SecurityGroupsUpdate", group.Id, group)
        End Sub
        Private Shared Sub OnUpdated_TransactionCallback(ByVal arg1 As String, ByVal arg2 As Integer, ByVal arg3 As Object)
            Manager.OnUpdated(CType(arg3, Entities.SecurityGroup))
        End Sub
        Public Shared Function GetByTypeAndSubType(ByVal type As Entities.SecurityGroupType, ByVal subType As Entities.SecurityGroupSubType) As Entities.SecurityGroup
            Return DBController.GetByTypeAndSubType(type, subType)
        End Function
        Public Shared Function GetAllByBuddyId(ByVal buddyId As Integer) As Generic.List(Of Entities.SecurityGroup)
            Return DBController.GetAllByBuddyId(buddyId)
        End Function
        Public Shared Function GetAll() As Generic.List(Of Entities.SecurityGroup)
            Return DBController.GetAll
        End Function
        Public Shared Function GetAllByIds(ByVal ids As Generic.HashSet(Of Integer)) As Generic.List(Of Entities.SecurityGroup)
            Dim aRet As New Generic.List(Of Entities.SecurityGroup)
            For Each clsG As Entities.SecurityGroup In GetAll()
                If ids.Contains(clsG.Id) Then aRet.Add(clsG)
            Next
            Return aRet
        End Function
        Public Shared Function GetAllMembers(ByVal groupId As Integer) As Generic.List(Of Entities.Buddy)
            Dim aIds As Generic.List(Of Integer)
            aIds = DBController.GetAllMemberIds(groupId)
            Return Buddies.Handler.GetAllByIds(aIds)
        End Function

        Public Shared Sub SetBuddyMembersList(ByVal buddyId As Integer, ByVal securityGroupIds As Generic.List(Of Integer), ByVal transactionScope As Data.TransactionScope)
            DBController.RemoveAllGroupMembershipsForBuddy(buddyId, transactionScope)
            For Each iId As Integer In securityGroupIds
                DBController.AddGroupMember(iId, buddyId, transactionScope)
            Next
        End Sub
        Public Shared Sub AddBuddyToGroup(ByVal buddyId As Integer, ByVal securityGroupId As Integer, ByVal transactionScope As Data.TransactionScope)
            DBController.AddGroupMember(securityGroupId, buddyId, transactionScope)
        End Sub
        Public Shared Sub SetGroupMembers(ByVal securityGroupId As Integer, ByVal buddyIds As Generic.List(Of Integer), ByVal transactionScope As Data.TransactionScope)
            DBController.RemoveAllMembers(securityGroupId, transactionScope)
            For Each iId As Integer In buddyIds
                DBController.AddGroupMember(securityGroupId, iId, transactionScope)
            Next
        End Sub
    End Class

    
End Namespace